<?php
// +----------------------------------------------------------------------
// | User: zq
// +----------------------------------------------------------------------
// | Time: 2021/12/21 15:34
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Wx;

use App\CodeResponse;
use App\Exceptions\BusinessException;
use App\Inputs\OrderSubmitInput;
use App\Services\Order\OrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Throwable;
use Yansongda\LaravelPay\Facades\Pay;

class OrderController extends WxController
{
    protected $except = ['wxNotify'];

    /**
     * 提交订单
     *
     * 1. 创建订单表项和订单商品表项;
     * 2. 购物车清空;
     * 3. 优惠券设置已用;
     * 4. 商品货品库存减少;
     * 5. 如果是团购商品,则创建团购活动表项.
     *
     * @return JsonResponse
     * @throws BusinessException
     * @throws Throwable
     */
    public function submit()
    {
        $input = OrderSubmitInput::new();

        $lockKey = sprintf('order_submit_%s_%s', $this->userId(), md5(serialize($input)));
        $lock = Cache::lock($lockKey, 5);
        if (!$lock->get()) {
            return $this->fail(CodeResponse::FAIL, '请勿重新请求');
        }

        $order = DB::transaction(function () use ($input) {
            return OrderService::getInstance()->submit($this->userId(), $input);
        });
        return $this->success([
            'orderId' => $order->id,
            'grouponLikeId' => $input->grouponLinkId ?? 0
        ]);
    }

    /**
     * 用户主动取消订单
     * @return JsonResponse
     * @throws BusinessException
     */
    public function cancel()
    {
        $orderId = $this->verifyId('orderId');
        OrderService::getInstance()->userCancel($this->userId(), $orderId);
        return $this->success();
    }

    /**
     * 申请退款
     * @return JsonResponse
     * @throws BusinessException
     */
    public function refund()
    {
        $orderId = $this->verifyId('orderId');
        OrderService::getInstance()->refund($this->userId(), $orderId);
        return $this->success();
    }

    /**
     * 确认收货
     * @return JsonResponse
     * @throws BusinessException
     */
    public function confirm()
    {
        $orderId = $this->verifyId('orderId');
        OrderService::getInstance()->confirm($this->userId(), $orderId);
        return $this->success();
    }

    // 订单详情
    public function detail()
    {
        $orderId = $this->verifyId('orderId');
        $detail = OrderService::getInstance()->detail($this->userId(), $orderId);
        return $this->success($detail);
    }

    // 创建订单
    public function h5pay()
    {
        $orderId = $this->verifyId('orderId');
        $order = OrderService::getInstance()->getWxPayOrder($this->userId(), $orderId);
        return Pay::wechat()->wap($order);
    }

    // 微信支付回调
    public function wxNotify()
    {
        $data = Pay::wechat()->verify();
        $data = $data->toArray();

        \Log::info('wxNotify', $data);
        DB::transaction(function () use ($data) {
            OrderService::getInstance()->wxNotify($data);
        });

        return Pay::wechat()->success();
    }
}